package com.kaiyidiannao.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.kaiyidiannao.common.BaseActionBarActivity;
import com.kaiyidiannao.data.DataCenter;
import com.kaiyidiannao.main.R;
import com.kaiyidiannao.model.bean.SecondHandCar;
import com.kaiyidiannao.model.req.model.GetCloseOpen;
import com.kaiyidiannao.view.ScaleImageView;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by mark on 2014/4/16.
 */
public class MySecondHandCarAdapter extends ImageLoaderBaseAdapter {

    private ImageLoader imageLoader;
    private LayoutInflater layoutInflater;
    private List<SecondHandCar> list = new ArrayList<SecondHandCar>();
    private String[] imgs;
    private Context context;
    private int tip = 0;

    public MySecondHandCarAdapter(Context context, List<SecondHandCar> list,int tip) {
        imageLoader = ImageLoader.getInstance();
        imageLoader.init(ImageLoaderConfiguration.createDefault(context));
        this.list = list;
        this.layoutInflater = LayoutInflater.from(context);
        this.context = context;
        this.tip = tip;
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        TextView mySecondHandCarItemNameText = null;
        TextView mySecondHandCarItemPriceText = null;
        TextView mySecondHandCarItemHowNewText = null;
        TextView mySecondHandCarItemCarAgeText = null;
        TextView mySecondHandCarItemMileAgeText = null;
        ImageButton mySecondHandCarItemButton = null;
        TextView publishTime;
        LinearLayout call = null;
        ScaleImageView imageView = null;
        TextView phoneNum = null;
        final SecondHandCar secondHandCar = list.get(position);
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.mysecondhand_car_item, null);
        }
        imageView = (ScaleImageView) convertView.findViewById(R.id.SecondHandCar_item_ImageView);
        mySecondHandCarItemNameText = (TextView) convertView.findViewById(R.id.mySecondHandCarItemNameText);
        mySecondHandCarItemPriceText = (TextView) convertView.findViewById(R.id.mySecondHandCarItemPriceText);
        mySecondHandCarItemHowNewText = (TextView) convertView.findViewById(R.id.mySecondHandCarItemHowNewText);
        mySecondHandCarItemCarAgeText = (TextView) convertView.findViewById(R.id.mySecondHandCarItemCarAgeText);
        mySecondHandCarItemButton = (ImageButton) convertView.findViewById(R.id.mySecondHandCarItemButton);
        mySecondHandCarItemMileAgeText = (TextView)convertView.findViewById(R.id.mySecondHandCarItemMileAgeText);
        phoneNum = (TextView) convertView.findViewById(R.id.phoneNum);
        call = (LinearLayout)convertView.findViewById(R.id.call);
        publishTime = (TextView)convertView.findViewById(R.id.publishTime);
//        imgs = (secondHandCar.getThumb()+",").split(",");
//        imageLoader.displayImage(secondHandCar.getThumb(), imageView);
        imageLoader.displayImage(secondHandCar.getThumb(),imageView, options, null);
        mySecondHandCarItemNameText.setText(secondHandCar.getTitle());
        if (secondHandCar.getPrice() == null) {
            mySecondHandCarItemPriceText.setText("无");
        } else {
            mySecondHandCarItemPriceText.setText(secondHandCar.getPrice() + "万");
        }
        if (secondHandCar.getMileage() == null){
            mySecondHandCarItemMileAgeText.setText("无");
        }else{
            mySecondHandCarItemMileAgeText.setText(secondHandCar.getMileage());
        }
        if(secondHandCar.getCondition() == null){
            mySecondHandCarItemHowNewText.setText("无");
        }else{
            mySecondHandCarItemHowNewText.setText(secondHandCar.getCondition());
        }

        mySecondHandCarItemCarAgeText.setText(secondHandCar.getCaryear()+"年");

        phoneNum.setText(secondHandCar.getTelephone());

        publishTime.setText(secondHandCar.getAddtime());

        if(list.get(position).getStatus()!=null){
            if (list.get(position).getStatus().equals("2")) {
                mySecondHandCarItemButton.setImageBitmap(BitmapFactory.decodeResource(convertView.getResources(), R.drawable.mypublish_dark_button));
            }else {
                mySecondHandCarItemButton.setImageBitmap(BitmapFactory.decodeResource(convertView.getResources(), R.drawable.mypublish_light_button));
            }

        }

        if (null != convertView) {
            if (null != mySecondHandCarItemButton) {

                mySecondHandCarItemButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        DataCenter.position = position;
                        ((BaseActionBarActivity)context).showProgressDialog();
                        if (Integer.parseInt(list.get(position).getStatus()) == 2) {
                            new GetCloseOpen(DataCenter.userLogin.get(0).getUserid(), "open", DataCenter.beansCar.get(position).getItemid(), "5", DataCenter.secondHandCar_category_id + "").execute();
                        } else if (Integer.parseInt(list.get(position).getStatus()) == 3) {
                            new GetCloseOpen(DataCenter.userLogin.get(0).getUserid(), "close", DataCenter.beansCar.get(position).getItemid(), "5", DataCenter.secondHandCar_category_id + "").execute();
                        }
                    }
                });
            }
            if (null != call){
                call.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String phoneNum =  secondHandCar.getTelephone();
                        Intent intent = new Intent();
                        intent.setAction(Intent.ACTION_CALL);
                        intent.setData(Uri.parse("tel:" + phoneNum));
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        context.startActivity(intent);
                    }
                });
            }
        }
        return convertView;
    }
}
